#include <iostream>
#include <fstream>
#include <string>
#include <cstdint>
#include <sstream>

int main()
{
    std::ifstream oldCCS;
    std::ofstream newCCS;
    std::string path;
    std::cout<<"请输入旧版CCS文件路径: ";
    std::cin>>path;
    std::stringstream targetPath;
    targetPath<<path.substr(0,path.find_last_of('/')+1)<<"NEW_"<<path.substr(path.find_last_of('/')+1);
    std::cout<<"目标文件路径为: "<<targetPath.str()<<std::endl;
    oldCCS.open(path,std::ios_base::in | std::ios_base::binary);
    if(!oldCCS.is_open())
    {
        std::cerr<<"无法打开旧的CCS文件，程序退出"<<std::endl;
        return -1;
    }
    newCCS.open(targetPath.str(),std::ios_base::binary | std::ios_base::out);
    if(!newCCS.is_open())
    {
        std::cerr<<"无法创建新的CCS文件，程序退出"<<std::endl;
        return -1;
    }
    uint32_t imageCount,imageHeight,imageWidth;
    oldCCS.read((char*)&imageCount,sizeof(uint32_t));
    oldCCS.read((char*)&imageWidth,sizeof(uint32_t));
    oldCCS.read((char*)&imageHeight,sizeof(uint32_t));
    newCCS.write((char*)&imageCount,sizeof(uint32_t));
    newCCS.write((char*)&imageWidth,sizeof(uint32_t));
    newCCS.write((char*)&imageHeight,sizeof(uint32_t));
    long imageSize=imageHeight*imageWidth;
    char* imageData=new char[imageSize];
    uint64_t time=0;
    for(int i=0;i<imageCount;i++)
    {
        oldCCS.read(imageData,imageSize);
        newCCS.write((char*)&time,sizeof(uint64_t));
        newCCS.write(imageData,imageSize);
        if(!(oldCCS.good() && newCCS.good()))
        {
            std::cerr<<"遇到意外的文件读取失败,程序终止!"<<std::endl;
            return -1;
        }
    }
    delete[] imageData;
    oldCCS.close();
    newCCS.close();
    return 0;
}